/**
 * 
 * purpose: section MODEL method collection...
 * @auth: Ning
 * date: 2009-8-20
 * 
 * */
package core.section
{
	import com.components.loader.I_Loader;
	import com.components.loader.LoaderCallback;
	
	import flash.events.EventDispatcher;
	
	public class SectionModel extends EventDispatcher
	{
		//START...
		
		//PROPERTY...
		
		private var loadingItem:LoaderCallback;
		
		private var queue:Array;		
		
		private var processing:Boolean;
		
		//CONSTRUCTOR...
		public function SectionModel()
		{
		
			queue = new Array;
						
		}
		
		//METHODS...
		
		//on item load complete...
		private function itemLoadComplete(content:*):void
		{
		
			processing = false;
			
			processLoaderItem();	
						
		}
		
		//process loader item...
		 protected function processLoaderItem():void
		{
			
			if(queue.length > 0)
			{
				
				processing = true;
				
				var item:LoaderCallback = queue.shift();
				
				loadingItem = item;
				
				item.addSuccessCallback(itemLoadComplete);
				
				item.addFailCallback(itemLoadComplete);
				
				item.target.load();				
									
			}	
			
		}			
		
		//load data...
		public function load(loader:I_Loader,callback:LoaderCallback,muti:Boolean = false):void
		{
						
			callback.target = loader;
			
			if(muti)
			{
			
				loader.load();
				
				return;
			
			}
						
			queue.push(callback)
			
			if(!processing)
			{
				
				processLoaderItem();
				
			}
						
		}
		
		//break...
		public function stopAllStream():void
		{
		
			if(loadingItem)
			{
				
				loadingItem.target.stop();
				
			}
			
			queue = [];
					
		}
		
		
		//END
	}
}